package descentFly.app
{
	import UIFree.controls.uif_ebutton;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_modalLayer extends df_block {
		private var _modalGurtain	: uif_ecomponent;
		private var _windowLayer	: uif_ecomponent;
		
		public function df_modalLayer(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
		}
		
		override protected function createComponents(layer:df_component, inner:Rectangle):void {
			super.createComponents(layer, inner);
			var inner: Rectangle = layer.getInner();
			_windowLayer = new uif_ecomponent(layer, inner.x, inner.y, inner.width, inner.height, style.windowLayer);
			
			_modalGurtain = new uif_ecomponent(layer, inner.x, inner.y, inner.width, inner.height, 
				style.gurtain?style.gurtain:'modalGurtain');
			_modalGurtain.visible = false;
			_modalGurtain.alpha = 0;
		}
		
		protected function toTopWindow(window: df_window): void {
			var topWindow: df_window = getWindowAt(windowCount - 1);
			if (topWindow != window) _windowLayer.swapChildren(topWindow, window);
		}
		
		protected function modalCount(): int {
			var count: int = 0;
			_windowLayer.forEach(function(window: df_window): void {
				if (window.modal && window.visible) count++;
			}, df_window);
			return count;
		}
		
		public override function addChild(child:DisplayObject):DisplayObject {
			if (child is df_window) {
				var window: df_window = child as df_window;
				_windowLayer.addChild(window);
				var inner: Rectangle = _windowLayer.EnterHere(new Point(window.width, window.height));
				window.position = inner.topLeft;
				window.addEvent(df_window.SHOW, doShowWindow);
				window.addEvent(df_window.CLOSE, doCloseWindow);
				window.addEvent(df_window.TOTOP, doToTopWindow);
			} else {
				var atIndex: int = (numChildren>0)?numChildren:0;
				if (_modalGurtain) atIndex--;
				if (_windowLayer) atIndex--;
				super.addChildAt(child, atIndex);
			}
			return child;
		}
		
		protected function changeModalMode(a_modal: Boolean): void {
			if (a_modal) _modalGurtain.slideShow();
			else _modalGurtain.slideHide(); 
		}
		
		protected function doShowWindow(e: Event): void {
			if (modalCount() == 1) changeModalMode(true);
		}
		
		protected function doToTopWindow(e: Event): void {
			toTopWindow(e.target as df_window);
		}
		
		protected function doCloseWindow(e: Event): void {
			if (modalCount() == 0) changeModalMode(false);
		}
		
/*		public function addWindow(a_window: df_window): void {
			_windowLayer.addChild(a_window);
		}
*/		
		public function getWindowAt(index: int): df_window {
			return _windowLayer.getChildAt(index) as df_window;
		}
		
		public function get windowCount(): int {
			return _windowLayer.numChildren;
		}
	}
}